﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PM.Web.Admin
{
    public partial class admin_website_newsclass_addup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["action"]))
                {
                    if (Request.QueryString["action"] == "del")
                    {
                        delData();
                    }
                }
                bindDDL();
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    this.hidId.Value = Request.QueryString["id"];
                    bindData();
                }

            }
        }

        private void bindDDL()
        {
            DataAccess.PmData dal = new DataAccess.PmData();
            this.ddlCommList.DataSource = dal.GetCommunity(int.Parse(Session["CommId"].ToString()), true);
            this.ddlCommList.DataBind();
        }

        private void delData()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                DataAccess.PmData dal = new DataAccess.PmData();
                if (dal.DelWebSiteNewsClass(int.Parse(Request.QueryString["id"])))
                {
                    Response.Write("ok");
                }
                else
                {
                    Response.Write("err");
                }
            }
            Response.End();
        }

        private void bindData()
        {
            if (this.hidId.Value != "0")
            {
               
                DataAccess.PmData dal = new DataAccess.PmData();
                System.Data.DataTable dt = dal.GetWebSiteNewsClass(int.Parse(hidId.Value));
                if (dt.Rows.Count > 0)
                {
                    this.ddlCommList.Items.FindByValue(dt.Rows[0]["self_community_code"].ToString()).Selected = true;
                    this.txtName.Text = dt.Rows[0]["name"].ToString();
                    this.txtSort.Text = dt.Rows[0]["sort"].ToString();
                }
            }
        }

        protected void cmdOk_Click(object sender, EventArgs e)
        {
            string sCommunity = this.ddlCommList.SelectedValue;
            string sName = this.txtName.Text.Trim();
            string sSort = this.txtSort.Text.Trim();
            int iSort;
            if (sName == string.Empty)
            {
                PM.Web.Common.JsAlert(this, "名称不能为空！");
                return;
            }
            if (!int.TryParse(sSort, out iSort))
            {
                PM.Web.Common.JsAlert(this, "排序为数字！");
                return;
            }
            DataAccess.PmData dal = new DataAccess.PmData();
            if (hidflag.Value == "0")
            {
                if (dal.AddWebSiteNewsClass(int.Parse(sCommunity),sName, iSort))
                {
                    PM.Web.Common.JsAlert(this, "保存成功！", true);
                }
            }
            else
            {
                if (dal.ModifyWebSiteNewsClass(int.Parse(hidId.Value), int.Parse(sCommunity), sName, iSort))
                {
                    PM.Web.Common.JsAlert(this, "修改成功！", true);
                }
            }
        }

    }
}